#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
  if(argc != 2) {
    printf("Usage:%s <file_descriptor> <shell_rediretor>\n",
           argv[0]);
    return 1;
  }
  int fd = atoi(argv[1]);
  int flags = fcntl(fd, F_GETFL);
  if(flags == -1) {
    perror("faild to get flags");
    return 1;
  }
  switch(flags & O_ACCMODE) {
  case O_RDONLY:
    printf("fd_%d read only\n",fd);
    break;
  case O_WRONLY:
    printf("fd_%d write only\n",fd);
    break;
  case O_RDWR:
    printf("fd_%d read and write\n",fd);
    break;
  default:
    fputs("Invalid accmode\n", stderr);
    return 2;
  }
  if(flags & O_APPEND)
     printf("fd_%d append\n",fd);
  if(flags & O_NONBLOCK)
     printf("fd_%d nonblock\n",fd);
  return 0;
}
